package xcmg.device.web.account;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.account.RefundCheckService;
import xcmg.device.service.vo.account.RefundCheckParam;
import xcmg.device.service.vo.account.RefundCheckVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

/**
 * Created by wrg on 2019/6/28.
 */
@RestController
@RequestMapping("/refundCheck")
@Api("退款单入口")
public class RefundCheckController extends BasicController{

    @Autowired
    private RefundCheckService refundCheckService;

    @PostMapping(value = "/list")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("获取退款录列表")
    public ActionResult list(@RequestBody SearchCommonVO<RefundCheckParam> condition)throws Exception{
        RefundCheckParam refundCheckParam = condition.getFilters();
        if(refundCheckParam == null){
            refundCheckParam = new RefundCheckParam();
        }
        refundCheckParam.setOrgId(getOrgId());
        condition.setFilters(refundCheckParam);
        PageCommonVO pageCommonVO = refundCheckService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("退款单结算新增")
    public ActionResult add(@RequestBody RefundCheckVO checkAddVO)throws Exception{
        if(null == checkAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        checkAddVO.setCreateId(getUserId());
        checkAddVO.setCreateName(getUserName());
        checkAddVO.setOrgId(getOrgId());
        try {
            return actionResult(refundCheckService.add(checkAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/reverse",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult reverse(@RequestBody RefundCheckVO refundCheckVO)throws Exception{
        if(null == refundCheckVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            return actionResult(refundCheckService.reverse(refundCheckVO.getId(),getOrgId(),getUserId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @PostMapping(value = "/export")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("退款单-按勾选导出")
    public void export( String[] ids)throws Exception {
        refundCheckService.export(Arrays.asList(ids));
    }

    @PostMapping(value = "/exportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("退款单-导出全部")
    public void exportAll(RefundCheckParam condition)throws Exception {
        if(null == condition){
            condition = new RefundCheckParam();
        }
        condition.setOrgId(getOrgId());
        refundCheckService.exportAll(condition);
    }
}
